package com.zto.tbschedule.console.controller;

import com.zto.tbschedule.console.help.LoginHelp;
import com.zto.tbschedule.console.service.IUserService;
import com.zto.tbschedule.console.util.ValidateUtil;
import com.zto.tbschedule.console.vo.Message;
import com.zto.tbschedule.console.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/login.do")
    @ResponseBody
    public Message<User> login(HttpServletRequest request,@RequestParam(value = "username",required = false)String username,@RequestParam(value = "password",required = false)String password){
        if(StringUtils.isEmpty(username)|| StringUtils.isEmpty(password)){
            return new Message<User>(false,"用户或密码为空");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        try {
            user=userService.login(user);
        }catch (Exception e){
            return new Message<User>(false,e.getMessage());
        }
        if(user==null){
            return new Message<User>(false,"登陆异常");
        }
        LoginHelp.setUser(request, user);
        return new Message<User>(true,"登陆成功",user);
    }
    @RequestMapping("/list.do")
    @ResponseBody
    public Message<List<User>> listUser(HttpServletRequest request){
        /*if(!LoginHelp.isAdmin(request)){
            return new Message<List<User>>(false,Message.ILLEGAL_OPERATION,"操作非法",null);
        }*/
        List<User> userList = userService.listUser();
        return new Message<List<User>>(true,"查询成功",userList);
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public Message<Void> addUser(HttpServletRequest request,@ModelAttribute User user) throws IOException {
        if(!LoginHelp.isAdmin(request)){
            return new Message<Void>(false,Message.ILLEGAL_OPERATION,"操作非法",null);
        }
        String checkError = ValidateUtil.validateModel(user);
        if(StringUtils.isEmpty(checkError)){
            return new Message<Void>(false,Message.PROCESS_EXCEPTION,checkError,null);
        }
        if(!user.getPassword().equals(user.getConfirPassword())){
            return new Message<Void>(false,Message.PROCESS_EXCEPTION,"确认密码与密码不相同",null);
        }
        try{
            userService.addUser(user);
            return new Message<Void>(true,Message.SUCCESS,"添加成功",null);
        }catch (Exception e){
            return new Message<Void>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }

    }


}
